package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.PurchaseListDao;
import com.atguigu.jxc.dao.PurchaseListGoodsDao;
import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.PurchaseListGoodsService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;
import java.util.Map;

@Service
public class PurchaseListGoodsServiceImpl implements PurchaseListGoodsService {
    @Autowired
    private PurchaseListGoodsDao purchaseListGoodsDao;

    @Autowired
    private SupplierDao supplierDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private PurchaseListDao purchaseListDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServiceVO save(PurchaseList purchaseList, String purchaseListGoodsStr,String purchaseNumber) {
        PurchaseList purchaseListVo = new PurchaseList();
        purchaseListVo.setPurchaseNumber(purchaseNumber);
        purchaseListVo.setAmountPaid(purchaseList.getAmountPaid());
        purchaseListVo.setAmountPayable(purchaseList.getAmountPayable());
        purchaseListVo.setPurchaseDate(purchaseList.getPurchaseDate());
        purchaseListVo.setRemarks(purchaseList.getRemarks());
        purchaseListVo.setState(purchaseList.getState());
        purchaseListVo.setSupplierId(purchaseList.getSupplierId());
        purchaseListVo.setUserId(1);// TODO 直接给了，不知道怎么获取

        Supplier supplier=supplierDao.getSupplierById(purchaseList.getSupplierId());
        purchaseListVo.setSupplierName(supplier.getSupplierName());

        User userById = userDao.getUserById(purchaseListVo.getUserId());//TODO
        purchaseListVo.setTrueName(userById.getTrueName());
        purchaseListDao.save(purchaseListVo);

        Gson gson = new Gson();
        PurchaseListGoods[] purchaseListGoods = gson.fromJson(purchaseListGoodsStr, PurchaseListGoods[].class);
        for (PurchaseListGoods purchaseListGood : purchaseListGoods) {
            PurchaseListGoods purchaseListGoodsVo = new PurchaseListGoods();
            purchaseListGoodsVo.setGoodsId(purchaseListGood.getGoodsId());
            purchaseListGoodsVo.setGoodsCode(purchaseListGood.getGoodsCode());
            purchaseListGoodsVo.setGoodsName(purchaseListGood.getGoodsName());
            purchaseListGoodsVo.setGoodsModel(purchaseListGood.getGoodsModel());
            purchaseListGoodsVo.setGoodsUnit(purchaseListGood.getGoodsUnit());
            purchaseListGoodsVo.setGoodsNum(purchaseListGood.getGoodsNum());
            purchaseListGoodsVo.setPrice(purchaseListGood.getPrice());
            purchaseListGoodsVo.setTotal(purchaseListGood.getTotal());
            purchaseListGoodsVo.setPurchaseListId(purchaseListVo.getPurchaseListId());
            purchaseListGoodsVo.setGoodsTypeId(purchaseListGood.getGoodsTypeId());
            purchaseListGoodsDao.save(purchaseListGoodsVo);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public List<PurchaseList> getList(String purchaseNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        List<PurchaseList> lists = purchaseListDao.getList(purchaseNumber, supplierId, state, sTime, eTime);
        for (PurchaseList list : lists) {
            Integer supplierIdVo = list.getSupplierId();
            Supplier supplierById = supplierDao.getSupplierById(supplierIdVo);
            list.setSupplierName(supplierById.getSupplierName());
            Integer userId = list.getUserId();
            User userById = userDao.getUserById(userId);
            list.setTrueName(userById.getTrueName());
        }
        return lists;
    }

    @Override
    public List<PurchaseListGoods> getGoodsList(Integer purchaseListId) {
        return purchaseListGoodsDao.getGoodsList(purchaseListId);

    }

    @Override
    public ServiceVO delete(Integer purchaseListId) {
        //先判断有没有详情
        List<PurchaseListGoods> goodsList = purchaseListGoodsDao.getGoodsList(purchaseListId);
        if (goodsList!=null){//详情有数据，循环删除
            for (PurchaseListGoods purchaseListGoods : goodsList) {
                Integer purchaseListGoodsId = purchaseListGoods.getPurchaseListGoodsId();
                purchaseListGoodsDao.deleteById(purchaseListGoodsId);
            }
        }
        //走到这，该returnListId在详情表一定是没有数据的
        purchaseListDao.deleteById(purchaseListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO updateState(Integer purchaseListId) {
        purchaseListDao.updateState(purchaseListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public List<Map<String,Object>> count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        List<Map<String,Object>> listMap=purchaseListGoodsDao.count(sTime,eTime,goodsTypeId,codeOrName);
        return listMap;
    }


}
